<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category/'+goods.categories[1].id">{{goods.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to="'/category/sub/'+goods.categories[0].id">{{goods.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem to="/">{{goods.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 商品信息左边 -->
        <div class="goods-info-left">
            <GoodsImage :images='goods.mainPictures'></GoodsImage>
            <GoodsSales></GoodsSales>
        </div>
        <!-- 商品信息右边 -->
        <div class="goods-info-right">
            <GoodsName :goods='goods'></GoodsName>

            <!-- 规格组件 -->
            <GoodsSku :key="goods.id" :goods='goods'  @change='changeSku'></GoodsSku>
            <!-- 数量组件 goods.inventory最大库存-->
            <XtxNumbox label='数量' v-model="count" :max='10'></XtxNumbox>
            <!-- 按钮 -->
            <XtxButton type='primary' style="margin-top:20px" @click="addToCartBtn">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id"/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs></GoodsTabs>
          <!-- 注意事项 -->
          <GoodsWarn></GoodsWarn>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type='1'></GoodsHot>
          <GoodsHot :goodsId="goods.id" :type='2'></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods'
import GoodsRelevant from './components/goods-relevant'
import { provide, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import XtxNumbox from '@/components/library/xtx-numbox.vue'
import XtxButton from '@/components/library/xtx-button.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import Message from '@/components/library/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, XtxNumbox, XtxButton, GoodsSku, GoodsTabs, GoodsHot, GoodsWarn },
  setup () {
    const route = useRoute()
    const goods = ref(null)
    // 导出
    provide('goods', goods)
    watch(() => route.params.id, async (id) => {
      if (route.path !== `/product/${id}`) return
      const { result } = await findGoods(id)
      //   console.log(res)
      goods.value = result
    }, { immediate: true })
    // 数量组件
    const count = ref(1)

    // 规格组件的回传
    const changeSku = (val) => {
      if (val.skuId) {
        goods.value.inventory = val.inventory
        goods.value.price = val.price
        goods.value.oldPrice = val.oldPrice
        goods.value.skuId = val.skuId
        goods.value.attrsText = val.attrsText
      } else {
        goods.value.skuId = ''
      }
    }
    const store = useStore()
    // 加入购物车
    const addToCartBtn = async () => {
      if (!goods.value.skuId) return Message({ type: 'warn', message: '请选择完整的规格' })
      const goodsItem = {
        id: goods.value.spuCode, // SPUID
        name: goods.value.name, // 商品名称
        price: goods.value.oldPrice, // 加入时价格
        nowPrice: goods.value.price, // 当前的价格
        skuId: goods.value.skuId, // SKUID
        attrsText: goods.value.attrsText, // 属性文字，例如“颜色:瓷白色 尺寸：8寸”
        stock: goods.value.inventory || goods.value.stock, // 库存
        count: count.value, // 🔔数量
        picture: goods.value.mainPictures[0], // 🔔图片
        selected: true, // 是否选中
        isEffective: true // 是否有效
      }
      console.log(goodsItem)
      // 基础写法 mutations 用 commit 调用
      // store.commit('cart/insertCart', goodsItem)
      // 高级写法 actions 用 dispatch 调用，dispatch 内部再调用 commit
      const res = await store.dispatch('cart/insertCart', goodsItem)
      Message({ type: 'success', message: res })
    }
    return { goods, count, changeSku, addToCartBtn }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .goods-info-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .goods-info-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
